using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Serviio.API;

namespace Serviio.WHS2011
{
    public partial class TabContainerControl : UserControl
    {
        private static readonly LogFacade log = LogFacade.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        ServiioConnection conn;

        private enum TabName
        {
            Status,
            Library,
            Advanced,
            About,
            Stopped,
            Unavailable,
            Upgrade,
            NotSet
        }

        StatusTabControl statusTabControl;
        LibraryTabControl libraryTabControl;
        AdvancedTabControl advancedTabControl;
        AboutTabControl aboutTabControl;
        StoppedTabControl stoppedTabControl;
        UnavailableTabControl unavailableTabControl;
        UpgradeTabControl upgradeTabControl;

        private ServiioServiceStatus currentStatus = ServiioServiceStatus.Unknown;

        private TabName currentTab = TabName.NotSet;

        public TabContainerControl()
        {
            InitializeComponent();
            log.Info("Initializing...");
        }

        public TabContainerControl(int width, int height)
            : this()
        {
            this.Width = width;
            this.Height = height;
        }

        private ServiioServiceStatus SetServiceStatus()
        {
            try
            {
                ServiioServiceStatus status = conn.ServiceStatus;

                if (status == currentStatus)
                    return status;

                if (status == ServiioServiceStatus.Started && !conn.IsAvailable)
                    return ServiioServiceStatus.Unavailable;

                bool local = Properties.Settings.Default.LocalServer;

                bool upgrade = conn.IsAvailable && !conn.CheckMinVersion(Properties.Settings.Default.MinServiioVersion);
                if (upgrade)
                    status = ServiioServiceStatus.Unavailable;

                currentStatus = status;

                log.Info("Changing service status to " + status.ToString());

                switch (status)
                {
                    case ServiioServiceStatus.Started:
                        buttonServiceStart.Enabled = true;
                        buttonServiceStart.Checked = true;
                        buttonServiceStart.Text = "Started";
                        buttonServiceStart.Image = Properties.Resources.Green16;
                        if (currentTab == TabName.NotSet ||
                            currentTab == TabName.Unavailable || 
                            currentTab == TabName.Upgrade || 
                            currentTab == TabName.Stopped)
                            ShowTab(TabName.Status);
                        break;
                    case ServiioServiceStatus.Stopped:
                        buttonServiceStart.Enabled = true;
                        buttonServiceStart.Checked = false;
                        buttonServiceStart.Text = "Stopped";
                        buttonServiceStart.Image = Properties.Resources.Red16;
                        ShowTab(TabName.Stopped);
                        break;
                    default:
                        buttonServiceStart.Enabled = false;
                        buttonServiceStart.Checked = false;
                        buttonServiceStart.Text = "Unavailable";
                        buttonServiceStart.Image = Properties.Resources.White16;
                        if (upgrade)
                            ShowTab(TabName.Upgrade);
                        else if (local)
                            ShowTab(TabName.Unavailable);
                        break;
                }

                return status;
            }
            catch (Exception ex)
            {
                log.Error(ex);
                return ServiioServiceStatus.Unknown;
            }
        }

        private void ShowTab(TabName tab)
        {
            try
            {
                tabStatus.Enabled = true;
                tabStatus.Checked = false;
                statusTabControl.Visible = false;

                tabLibrary.Enabled = true;
                tabLibrary.Checked = false;
                libraryTabControl.Visible = false;

                tabAdvanced.Enabled = true;
                tabAdvanced.Checked = false;
                advancedTabControl.Visible = false;

                tabAbout.Enabled = true;
                tabAbout.Checked = false;
                aboutTabControl.Visible = false;

                stoppedTabControl.Visible = false;

                unavailableTabControl.Visible = false;

                upgradeTabControl.Visible = false;

                switch (tab)
                {
                    case TabName.Status:
                        tabStatus.Checked = true;
                        statusTabControl.Visible = true;
                        statusTabControl.Initialize(conn);
                        break;
                    case TabName.Library:
                        tabLibrary.Checked = true;
                        libraryTabControl.Visible = true;
                        libraryTabControl.Initialize(conn);
                        break;
                    case TabName.Advanced:
                        tabAdvanced.Checked = true;
                        advancedTabControl.Visible = true;
                        advancedTabControl.Initialize(conn);
                        break;
                    case TabName.About:
                        tabAbout.Checked = true;
                        aboutTabControl.Visible = true;
                        aboutTabControl.Initialize(conn/*, consoleServices*/);
                        break;
                    case TabName.Stopped:
                        tabStatus.Enabled = false;
                        tabLibrary.Enabled = false;
                        tabAdvanced.Enabled = false;
                        tabAbout.Enabled = false;
                        stoppedTabControl.Visible = true;
                        stoppedTabControl.Initialize(conn);
                        break;
                    case TabName.Unavailable:
                        tabStatus.Enabled = false;
                        tabLibrary.Enabled = false;
                        tabAdvanced.Enabled = false;
                        tabAbout.Enabled = false;
                        unavailableTabControl.Visible = true;
                        break;
                    case TabName.Upgrade:
                        tabStatus.Enabled = false;
                        tabLibrary.Enabled = false;
                        tabAdvanced.Enabled = false;
                        tabAbout.Enabled = false;
                        upgradeTabControl.Visible = true;
                        break;
                }

                currentTab = tab;
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

        private void MainTabUserControl_Load(object sender, EventArgs e)
        {
            try
            {
                log.Info("Loading...");

                statusTabControl = new StatusTabControl();
                statusTabControl.Parent = tabContainerPanel;
                statusTabControl.Dock = DockStyle.Fill;
                statusTabControl.Visible = false;

                libraryTabControl = new LibraryTabControl();
                libraryTabControl.Parent = tabContainerPanel;
                libraryTabControl.Dock = DockStyle.Fill;
                libraryTabControl.Visible = false;

                advancedTabControl = new AdvancedTabControl();
                advancedTabControl.Parent = tabContainerPanel;
                advancedTabControl.Dock = DockStyle.Fill;
                advancedTabControl.Visible = false;

                aboutTabControl = new AboutTabControl();
                aboutTabControl.Parent = tabContainerPanel;
                aboutTabControl.Dock = DockStyle.Fill;
                aboutTabControl.Visible = false;

                stoppedTabControl = new StoppedTabControl();
                stoppedTabControl.Parent = tabContainerPanel;
                stoppedTabControl.Dock = DockStyle.Fill;
                stoppedTabControl.Visible = false;

                unavailableTabControl = new UnavailableTabControl();
                unavailableTabControl.Parent = tabContainerPanel;
                unavailableTabControl.Dock = DockStyle.Fill;
                unavailableTabControl.Visible = false;

                upgradeTabControl = new UpgradeTabControl();
                upgradeTabControl.Parent = tabContainerPanel;
                upgradeTabControl.Dock = DockStyle.Fill;
                upgradeTabControl.Visible = false;

                if (Properties.Settings.Default.LocalServer)
                {
                    conn = new ServiioConnection();
                }
                else
                {
                    conn = new ServiioConnection(Properties.Settings.Default.RemoteHost, Properties.Settings.Default.RemotePort);
                }

                SetServiceStatus();

                timerServiceStatus.Enabled = true;
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

        private void tabStatus_Click(object sender, EventArgs e)
        {
            try
            {
                if (!tabStatus.Checked)
                    ShowTab(TabName.Status);
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

        private void tabLibrary_Click(object sender, EventArgs e)
        {
            try
            {
                if (!tabLibrary.Checked)
                    ShowTab(TabName.Library);
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

        private void tabAdvanced_Click(object sender, EventArgs e)
        {
            try
            {
                if (!tabAdvanced.Checked)
                    ShowTab(TabName.Advanced);
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

        private void tabAbout_Click(object sender, EventArgs e)
        {
            try
            {
                if (!tabAbout.Checked)
                    ShowTab(TabName.About);
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

        private void timerServiceStatus_Tick(object sender, EventArgs e)
        {
            try
            {
                SetServiceStatus();
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }

        private void buttonServiceStart_Click(object sender, EventArgs e)
        {
            try
            {
                buttonServiceStart.Text = "...wait...";
                this.Cursor = Cursors.WaitCursor;
                timerServiceStatus.Enabled = false;
                Application.DoEvents();

                if (!buttonServiceStart.Checked)
                    conn.StartServiioService();
                else
                    conn.StopServiioService();

                SetServiceStatus();
                timerServiceStatus.Enabled = true;
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
            finally
            {
                this.Cursor = Cursors.Arrow;
            }
        }
    }
}
